using CQRSDemo.Application.Orders.Commands.CreateOrder;
using CQRSDemo.Application.Orders.Commands.UpdateOrder;
using CQRSDemo.Application.Orders.Queries.GetOrder;
using CQRSDemo.Application.Orders.Queries.GetOrders;
using CQRSDemo.Application.Orders.Queries.Response;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CQRSDemo.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
    private readonly IMediator _mediator;

    public OrderController(IMediator mediator)
    {
        _mediator = mediator;
    }

    [HttpPost]
    public async Task<ActionResult> CreateOrder(CreateOrderCommand command)
    {
        var id = await _mediator.Send(command);
        return CreatedAtRoute("GetOrderById", new { Id = id }, id);
    }

    [HttpPut]
    public async Task<int> UpdateOrder(UpdateOrderCommand command)
    {
        return await _mediator.Send(command);
    }

    [HttpGet("{id}", Name = "GetOrderById")]
    public async Task<OrderResponse> GetOrder(int id)
    {
        return await _mediator.Send(new GetOrderQuery()
        {
            Id = id
        });
    }

    [HttpGet]
    public async Task<List<OrderResponse>> GetOrders()
    {
        return await _mediator.Send(new GetOrdersQuery());
    }
}
